package org.example.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.enums.BizCodeEnum;
import org.example.exception.BizException;
import org.example.mapper.UserMapper;
import org.example.model.AddressDO;
import org.example.request.AddressAddRequest;
import org.example.service.AddressService;
import org.example.util.JsonData;
import org.example.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author LinN
 * @since 2022-09-25
 */
@RestController
@RequestMapping("/api/address/v1")
@Api(tags = "收货地址模块")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("根据id查询地址详情")
    @GetMapping("/find/{address_id}")
    public JsonData detail(@ApiParam(value = "地址id", required = true) @PathVariable("address_id") Long addressId) {
        AddressVO addressVO = addressService.findById(addressId);
        return JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("新增地址")
    @PostMapping("add")
    public JsonData add(@ApiParam(value = "新增地址对象", required = true)
                        @RequestBody AddressAddRequest addressAddRequest) {
        addressService.add(addressAddRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("删除地址")
    @DeleteMapping("/del/{address_id}")
    public JsonData del(@ApiParam(value = "地址id", required = true)
                        @PathVariable("address_id") Long addressId) {
        int rows = addressService.deleteById(addressId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_ERROR);
    }

    @ApiOperation("用户的所有地址")
    @GetMapping("/list")
    public JsonData list() {
        List<AddressVO> addressVOList = addressService.list();
        return JsonData.buildSuccess(addressVOList);
    }

}

